import { getCurrentInstance } from 'vue'
import { showMessage, baseMessage, message, closeMessage, clearMessageQueue } from '@/utils/message';
import type { MessageOptions } from '@/components/BaseMessage.vue';

/**
 * 消息Hook - 提供Composition API方式使用消息功能
 */
export function useMessage() {
  const instance = getCurrentInstance()
  /**
   * 显示消息
   */
  const show = (msg: string, type: 'success' | 'error' | 'warning' | 'info' = 'info', options?: Partial<MessageOptions>) => {
    showMessage(msg, type, options);
  };

  /**
   * 基础消息方法 - 兼容原有调用方式
   */
  const base = (msg: string, type: 'success' | 'error' | 'warning' | 'info' = 'info', options?: Partial<MessageOptions>) => {
    baseMessage(msg, type, options);
  };

  /**
   * 成功消息 - 优先使用全局方法
   */
  const success = (msg: string, options?: Partial<MessageOptions>) => {
    if (instance?.proxy?.$showSuccess) {
      instance.proxy.$showSuccess(msg, options);
    } else {
      message.success(msg, options);
    }
  };

  /**
   * 错误消息 - 优先使用全局方法
   */
  const error = (msg: string, options?: Partial<MessageOptions>) => {
    if (instance?.proxy?.$showError) {
      instance.proxy.$showError(msg, options);
    } else {
      message.error(msg, options);
    }
  };

  /**
   * 警告消息
   */
  const warning = (msg: string, options?: Partial<MessageOptions>) => {
    message.warning(msg, options);
  };

  /**
   * 信息消息
   */
  const info = (msg: string, options?: Partial<MessageOptions>) => {
    message.info(msg, options);
  };

  /**
   * 关闭当前消息
   */
  const close = () => {
    closeMessage();
  };

  /**
   * 清空消息队列
   */
  const clear = () => {
    clearMessageQueue();
  };

  // 直接暴露全局方法的简便访问方式
  const showSuccess = (msg: string, options?: Partial<MessageOptions>) => {
    if (instance?.proxy?.$showSuccess) {
      instance.proxy.$showSuccess(msg, options);
    } else {
      message.success(msg, options);
    }
  };

  const showError = (msg: string, options?: Partial<MessageOptions>) => {
    if (instance?.proxy?.$showError) {
      instance.proxy.$showError(msg, options);
    } else {
      message.error(msg, options);
    }
  };

  return {
    show,
    base,
    success,
    error,
    warning,
    info,
    close,
    clear,
    showSuccess,
    showError
  };
}

export default useMessage; 